//
//  WmcCreateTagInputView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/17.
//

import UIKit
import YPBedrock

// MARK: - [WmcCreateTagInputView]

class WmcCreateTagInputView: WmcBaseView {
    lazy var tagColorSelectView: WmcTagColorSelectView = {
        let collectionView = WmcTagColorSelectView()
        return collectionView
    }()

    lazy var tagStyleSelectView: WmcTagStyleSelectView = {
        let collectionView = WmcTagStyleSelectView()
        return collectionView
    }()

    lazy var saveBtn: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .wmc_mainTheme
        btn.setTitle("确定", for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.titleLabel?.font = 14.wmc_font
        btn.layer.cornerRadius = 4
        btn.layer.masksToBounds = true
        btn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.doneBlock?()
        }
        return btn
    }()

    lazy var textView: UITextView = {
        let textView = UITextView()
        textView.font = 16.yp.uiFont(weight: .medium)
        textView.textColor = .wmc_grayH2_323233
        textView.contentInset = .zero
        textView.backgroundColor = .clear
        textView.returnKeyType = .done
        textView.delegate = self
        textView.layoutManager.delegate = self
        return textView
    }()

    lazy var inputTextView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    var textDidChangeBlock: YPBlock.OneParam<String?>?
    
    var doneBlock: YPBlock.Empty?

    
    override func makeUI() {
        backgroundColor = .clear

        addSubview(inputTextView)
        inputTextView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(54.wmc_autoVer)
        }

        inputTextView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.equalTo(52.wmc_autoHor)
            make.height.equalTo(32)
            make.right.equalTo(-Wmc_standardEdge.right)
        }

        inputTextView.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.left.equalTo(6)
            make.right.equalTo(saveBtn.snp.left).offset(-6)
            make.top.bottom.equalToSuperview()
        }

        addSubview(tagStyleSelectView)
        tagStyleSelectView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(inputTextView.snp.top)
            make.height.equalTo(74.wmc_autoVer)
        }

        addSubview(tagColorSelectView)
        tagColorSelectView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(Wmc_standardEdge.left)
            make.right.equalTo(-Wmc_standardEdge.right)
            make.bottom.equalTo(tagStyleSelectView.snp.top)
        }
    }
    
    func setText(_ text: String) {
        textView.text = text
        textViewDidChange(textView)
    }
}

// MARK: - UITextViewDelegate

extension WmcCreateTagInputView: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        guard var text = textView.text else { return }
        text = text.yp.replacing("\n", with: "")
        if text.count > 100 {
            text = text.yp.keepPrefix(count: 100)
            textView.text = text
        }
        textDidChangeBlock?(text)
    }

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            textView.resignFirstResponder()
            doneBlock?()
            return false
        }
        return true
    }
}

// MARK: - NSLayoutManagerDelegate

extension WmcCreateTagInputView: NSLayoutManagerDelegate {
    /// 行间距
    func layoutManager(_ layoutManager: NSLayoutManager, lineSpacingAfterGlyphAt glyphIndex: Int, withProposedLineFragmentRect rect: CGRect) -> CGFloat {
        return 5
    }
}
